#include "gobang.h"
#include "ui_gobang.h"
#include "qpainter.h"
#include "qevent.h"
#include"qpoint.h"
#include "qmessagebox.h"
#include<qtimer.h>
#include<QPushButton>

gobang::gobang(QWidget *parent) :
    QMainWindow(parent),
    limit_time(30),
    timer(new QTimer(this)),

    ui(new Ui::gobang)
{
    ui->setupUi(this);
    ui->mainToolBar->hide();
    ui->menuBar->hide();
    this->setGeometry(QRect(400,100,(CHESS_COLUMES+10)*RECT_WIDTH,(CHESS_ROWS+1)*RECT_HEIGHT));
    mIsBlackTurn=true;
    mIsWhichBg=true;
    connect(timer,SIGNAL(timeout()),this,SLOT(countdown()));
    temp_time=limit_time+1;
    timer->start(1000);
}

gobang::~gobang()
{
    delete ui;
}
void gobang::paintEvent(QPaintEvent *e){
    DrawChessboard();
    DrawItems();
    DrawItemWithMouse();

    update();
}

void gobang::DrawChessboard(){
    QPainter painter(this);


    //painter.setRenderHint(QPainter::HighQualityAntialiasing,true);
    //painter.setPen(Qt::black);
    //painter.setBrush(Qt::white);
    painter.setPen(QPen(QColor(Qt::black),2));
    //painter.drawRect(0.5*RECT_WIDTH,0.5*RECT_WIDTH,575,375);
    if(mIsWhichBg)
    {
        painter.drawPixmap(0.5*RECT_WIDTH,0.5*RECT_WIDTH,575,375,QPixmap(":pic/img/bg1.jpg"));
    }
    else
    {
        painter.drawPixmap(0.5*RECT_WIDTH,0.5*RECT_WIDTH,575,375,QPixmap(":pic/img/bg2.jpg"));
    }


    for(int i=0;i<CHESS_COLUMES;i++){
        for(int j=0;j<CHESS_ROWS;j++){
            painter.drawRect((i+0.5)*RECT_WIDTH,(j+0.5)*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
        }
    }
    //附加显示
    painter.drawRect(387.5,12.5,200,375);
    //黑棋
    QString BLACK="黑   棋";
    QFont font1("Courier",15);
    painter.setFont(font1);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(440,60,BLACK);

    //白棋
    QString WHITE="白   棋";
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(440,180,WHITE);

    if(mIsBlackTurn)
    {
       painter.drawText(470,110,str);
       painter.drawText(470,230,"0");
    }
    if(!mIsBlackTurn)
    {
        painter.drawText(470,230,str);
        painter.drawText(470,110,"0");
    }
}
bool gobang::countdown()
{
    temp_time--;
    str=tr("%1").arg(temp_time);

    if(temp_time==0)
    {
        timer->stop();
        if(!mIsBlackTurn)//白棋胜利
        {
            //QSound::play("../my_gobang/sound/lose.wav");
            QString mytitle="消息框";
            QString myinfo="超时！黑棋胜利！";
            QMessageBox::information(this,mytitle,myinfo);
            //player->stop();
            this->hide();
        }
        else//黑棋胜利
        {
            //QSound::play("../my_gobang/sound/lose.wav");
            QString mytitle="消息框";
            QString myinfo="超时！白棋胜利！";
            QMessageBox::information(this,mytitle,myinfo);
            //player->stop();
            this->hide();
        }
        return true;
    }
    return false;
}
void gobang::DrawItems(){
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));

    for(int i=0;i<mItems.size();i++){
        Item item=mItems[i];
        if(item.mBlack){
            painter.setBrush(Qt::black);

        }
        else{
            painter.setBrush(Qt::white);
        }
        DrawChessAtPoint(painter,item.mPt);
    }
}
void gobang::DrawChessAtPoint(QPainter& painter,QPoint& pt){

    QPoint ptCenter((pt.x()+0.5)*RECT_WIDTH,(pt.y()+0.5)*RECT_HEIGHT);
    painter.drawEllipse(ptCenter,RECT_WIDTH/2,RECT_HEIGHT/2);
}

void gobang::DrawItemWithMouse(){
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));

    if(mIsBlackTurn){
        painter.setBrush(Qt::black);
    }
    else{
        painter.setBrush(Qt::white);
    }

    painter.drawEllipse(mapFromGlobal(QCursor::pos()),RECT_WIDTH / 2,RECT_HEIGHT / 2);
}
int gobang::CountItems(Item item,QPoint ptDirection){
    int n=0;
    item.mPt+=ptDirection;

    while(mItems.contains(item)){
        n++;
        item.mPt+=ptDirection;
    }
    return n;
}
void gobang::mousePressEvent(QMouseEvent*e){

    QPoint pt;
    pt.setX( (e->pos().x() ) / RECT_WIDTH);
    pt.setY( (e->pos().y() ) / RECT_HEIGHT);

    for(int i=0;i<mItems.size();i++){
            Item item=mItems[i];
            if(item.mPt==pt){
                return;
            }
        }

    Item item(pt,mIsBlackTurn);
    count++;
    mItems.append(item);
    temp_time=limit_time;

    int nLeft=CountItems(item,QPoint(-1,0));
    int nLeftUp=CountItems(item,QPoint(-1,-1));
    int nLeftDown=CountItems(item,QPoint(-1,1));
    int nRightUp=CountItems(item,QPoint(1,-1));
    int nRight=CountItems(item,QPoint(1,0));
    int nRightDown=CountItems(item,QPoint(1,1));
    int nUp=CountItems(item,QPoint(0,-1));
    int nDown=CountItems(item,QPoint(0,1));

        if((nLeft+nRight)>=4||(nUp+nDown)>=4||(nLeftUp+nRightDown)>=4||(nLeftDown+nRightUp)>=4){
            timer->stop();
            //QString str=mIsBlackTurn?"黑棋胜利！":"白棋胜利！";
            //QMessageBox::information(NULL,"GAME OVER",str,QMessageBox::Yes,QMessageBox::No);

            QMessageBox msgbox;
            msgbox.setText("游戏结束");
            if(mIsBlackTurn){
                msgbox.setInformativeText("黑棋胜利！");
            }
            else{
                msgbox.setInformativeText("白棋胜利！");
            }
            QPushButton* Yesbtn=msgbox.addButton(QMessageBox::Yes);
            msgbox.addButton(QMessageBox::No);
            msgbox.exec();
            if(msgbox.clickedButton()==Yesbtn){
                mItems.clear();
                timer->start(1000);

            }
            else{
                mIsBlackTurn=true;
                this->close();
                msgbox.close();
            }

            return;
        }

    mIsBlackTurn = !mIsBlackTurn;
}



void gobang::on_pushButton_clicked()
{
    mItems.clear();
    mIsBlackTurn=true;
    temp_time=limit_time+1;
    update();
}

void gobang::on_pushButton_2_clicked()
{
    mIsWhichBg=!mIsWhichBg;
}

void gobang::on_pushButton_3_clicked()
{
    if(count==0)
    {
        QString mytitle="消息框";
        QString myinfo="还未开局！";
        QMessageBox::information(this,mytitle,myinfo);
    }
    else
    {
    QPoint pt1(1000,1000);
    mItems[count-1].mPt=pt1;
    mIsBlackTurn=!mIsBlackTurn;
    temp_time=limit_time+1;
    }
}
